package com.ruoyi.web.controller.admin.change;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.DemandChangeRequestAuditDTO;
import com.ruoyi.system.dto.DemandChangeRequestCreateDTO;
import com.ruoyi.system.dto.DemandChangeRequestQueryDTO;
import com.ruoyi.system.service.IDemandChangeRequestService;
import com.ruoyi.system.vo.DemandChangeRequestVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设计师需求变更管理控制器
 * 
 * @author ruoyi
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/admin/change/demand")
public class DemandChangeController extends BaseController {

    @Resource
    private IDemandChangeRequestService changeRequestService;

    /**
     * 保存草稿
     */
    @PostMapping("/draft/save")
    public AjaxResult saveDraft(@Validated @RequestBody DemandChangeRequestCreateDTO createDTO) {
        Long designerId = getCurrentUserId();
        boolean result = changeRequestService.createDraft(createDTO, designerId);
        return AjaxResult.success(result);
    }

    /**
     * 提交审核
     */
    @PostMapping("/submit/{requestId}")
    public AjaxResult submitForAudit(@PathVariable Long requestId) {
        Long designerId = getCurrentUserId();
        boolean result = changeRequestService.submitForAudit(requestId, designerId);
        return AjaxResult.success(result);
    }

    /**
     * 更新草稿
     */
    @PutMapping("/draft/{requestId}")
    public AjaxResult updateDraft(@PathVariable Long requestId, @Validated @RequestBody DemandChangeRequestCreateDTO createDTO) {
        Long designerId = getCurrentUserId();
        boolean result = changeRequestService.updateDraft(requestId, createDTO, designerId);
        return AjaxResult.success(result);
    }

    /**
     * 查询变更申请列表
     * 设计师：查询自己提交的需求变更
     * 管理员：查询待审核的需求变更
     */
    @PostMapping("/my-list")
    public TableDataInfo getMyChangeRequestList(@RequestBody DemandChangeRequestQueryDTO queryDTO) {
        startPage();
        List<DemandChangeRequestVO> list = changeRequestService.getChangeRequestList(queryDTO);
        return getDataTable(list);
    }

    /**
     * 获取变更申请详情
     */
    @GetMapping("/{requestId}")
    public AjaxResult getChangeRequestDetail(@PathVariable Long requestId) {
        DemandChangeRequestVO detail = changeRequestService.getChangeRequestDetail(requestId);
        return AjaxResult.success(detail);
    }

    /**
     * 通过审核
     */
    @PostMapping("/approve")
    public AjaxResult approveChangeRequest(@Validated @RequestBody DemandChangeRequestAuditDTO auditDTO) {
        Long auditorId = getCurrentUserId();
        boolean result = changeRequestService.approveChangeRequest(auditDTO, auditorId);
        return AjaxResult.success(result);
    }

    /**
     * 驳回审核
     */
    @PostMapping("/reject")
    public AjaxResult rejectChangeRequest(@Validated @RequestBody DemandChangeRequestAuditDTO auditDTO) {
        Long auditorId = getCurrentUserId();
        boolean result = changeRequestService.rejectChangeRequest(auditDTO, auditorId);
        return AjaxResult.success(result);
    }

    /**
     * 取消变更申请
     */
    @PostMapping("/cancel/{requestId}")
    public AjaxResult cancelChangeRequest(@PathVariable Long requestId) {
        Long designerId = getCurrentUserId();
        boolean result = changeRequestService.cancelChangeRequest(requestId, designerId);
        return AjaxResult.success(result);
    }

    /**
     * 获取当前用户ID
     */
    private Long getCurrentUserId() {
        return SecurityUtils.getUserId();
    }
}
